﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Blog.Models;
using Blog.Models.CommentServices;
using Spirit.Portal.Core.ImageServices;
using Spirit.Portal.Core.Web;

namespace Blog.Controllers
{
    public class SettingController : Controller
    {
        //
        // GET: /Setting/
        DataDataContext db=new DataDataContext();
        private IImageService imageService = new ImageService();
       
        
        [GateAuthorize(Roles = "admin")]
        public ActionResult Index()
        {
            var model = (from c in db.Settings
                         select c).FirstOrDefault();
            SettingModel model1=new SettingModel();
            model1.Title = model1.GetByName("Title").Value;
            model1.Status = model1.GetByName("Status").Value;
            model1.Footer = model1.GetByName("Footer").Value;
            BindDdlCreatedById("-1");
            return View(model1);
        }
        [HttpPost]
        [GateAuthorize(Roles = "admin")]
        public ActionResult Index(SettingModel model, HttpPostedFileBase fileUpload,string btnthem)
        {
            if (btnthem != null)
            {
                if (fileUpload != null)
                {
                    if (Helper.Helper.IsImage(fileUpload.FileName))
                    {


                        try
                        {

                            fileUpload.SaveAs(Server.MapPath("~/Content/")+fileUpload.FileName);
                            SettingModel model1 = new SettingModel();
                            model1.GetByName("ImageBackground").Value = fileUpload.FileName;
                            model1.Save();


                        }
                        catch (Exception ex)
                        {
                        }
       
    
                    }
                    else
                    {
                        ModelState.AddModelError("anh", "Chưa đúng định dạng/dung lượng quá lớn");
                    }
                }
            if (ModelState.IsValid)
            {
                SettingModel model1 = new SettingModel();
                model1.GetByName("Title").Value = model.Title;

                model1.GetByName("Footer").Value = model.Footer;
                model1.GetByName("Status").Value = model.Status;
                model1.GetByName("CatalogAdd").Value = model.CatalogAdd.ToString();
                if (model.CatalogAdd != -1)
                {
                    (from c in db.Catalogs
                     where c.CatalogId == model.CatalogAdd
                     select c).FirstOrDefault().Status = true;
                }

                model1.GetByName("CatalogDelete").Value= model.CatalogDelete.ToString();

                if (model.CatalogDelete != -1)
                {
                    (from c in db.Catalogs
                     where c.CatalogId == model.CatalogDelete
                     select c).FirstOrDefault().Status = false;

                }
                model1.Save();
                db.SubmitChanges();
                return RedirectToAction("Index", "Home");
            }
        }
    BindDdlCreatedById("-1");
    return View();
            
        }
        private void BindDdlCreatedById(string createdById)
        {

            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem { Text = "- Chọn một field -", Value = "-1", Selected = "-1" == createdById.ToString() });
            list.AddRange(db.Catalogs.Select(c => new SelectListItem
                                                      {
                                                          Text = c.CatalogName,
                                                          Value = c.CatalogId.ToString(),
                                                          Selected = createdById == c.CatalogId.ToString()
                                                      }));
            ViewData["createdById"] =list;
        }
    }
}
